package com.CmJava.utils;

import com.CmJava.entity.blog.BlogModel;
import com.CmJava.entity.blog.BlogRedis;
import com.CmJava.entity.project.ChildModel;
import com.CmJava.entity.project.Project;
import com.CmJava.entity.project.ProjectModel;

public class OldProjectParseUtil {
    //转换旧项目
    public static String parseOldProject(Project project){
        String projectRedisData = "[TOCM]\n";
        projectRedisData+="# 前端：\n"+project.getFrontTech()+"\n";
        projectRedisData+="# 后端：\n"+project.getBackTech()+"\n";
        projectRedisData+="# 项目模块：\n";

        for (ProjectModel projectModel : project.getModel()) {
            //清除标题格式
            String modelName = projectModel.getModelName();
            modelName = modelName.replaceAll("<span style=\"font-size:120%;color:black\">", "");
            modelName = modelName.replaceAll("<span style=\"font-size:180%;color:black\">", "");
            modelName = modelName.replaceAll("</span>", "");
            modelName = modelName.replaceAll("&nbsp;", "");

            projectRedisData += "## " + modelName + "\n";

            for (ChildModel childModel : projectModel.getChildModel()) {

                String childModelName = childModel.getChildModelName();
                childModelName = childModelName.replaceAll("&nbsp;", "");

                String childModelDesc = childModel.getChildModelDesc();
                //清除换行
                childModelDesc = childModelDesc.replaceAll("<br><br>&nbsp;&nbsp;&nbsp;&nbsp;", "");
                childModelDesc = childModelDesc.replaceAll("<br>&nbsp;&nbsp;&nbsp;&nbsp;", "");
                childModelDesc = childModelDesc.replaceAll("&nbsp;", "");

                //置换代码块
                String oldCodeHead = "<pre class=\"layui-code\" lay-title=\"...代码块标题...\">";
                String oldCodeTail = "</pre>";
                while (true) {

                    int index = childModelDesc.indexOf(oldCodeHead);
                    int index1 = childModelDesc.indexOf(oldCodeTail);
                    if (index == -1 || index1 == -1) {
                        break;
                    }
                    childModelDesc = childModelDesc.replaceAll(oldCodeHead, "```java");
                    childModelDesc = childModelDesc.replaceAll(oldCodeTail, "```");
                }

                childModelDesc.replaceAll("", "");

                projectRedisData += "### " + childModelName + "\n";
                projectRedisData += childModelDesc + "\n";
                if (!childModel.getChildModelImg().equals("--")) {
                    projectRedisData += "![image](" + childModel.getChildModelImg() + ")\n";
                }
                projectRedisData += "\n";
            }
        }

        return projectRedisData;
    }
}
